package com.xuecheng.learning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.model.RestResponse;
import com.xuecheng.content.model.po.CoursePublish;
import com.xuecheng.learning.feignclient.ContentServiceClient;
import com.xuecheng.learning.feignclient.MediaServiceClient;
import com.xuecheng.learning.mapper.XcCourseTablesMapper;
import com.xuecheng.learning.model.dto.XcCourseTablesDto;
import com.xuecheng.learning.model.po.XcCourseTables;
import com.xuecheng.learning.service.LearningService;
import com.xuecheng.learning.service.XcChooseCourseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author xiao
 * @Date 2024/12/27 上午 9:14
 * @ClassName: LearningServiceImpl
 * @Description: 获取学习资源
 */
@Service
@Slf4j
public class LearningServiceImpl implements LearningService {
    @Resource
    private ContentServiceClient contentServiceClient;
    @Resource
    private XcChooseCourseService xcChooseCourseService;
    @Resource
    private MediaServiceClient mediaServiceClient;

    /**
     * @param userId
     * @param courseId    课程id
     * @param teachplanId 课程计划id
     * @param mediaId     视频文件id
     * @return com.xuecheng.base.model.RestResponse<java.lang.String>
     * @description 获取教学视频
     * @author Mr.M
     * @date 2022/10/5 9:08
     */
    @Override
    public RestResponse<String> getVideo(String userId, Long courseId, Long teachplanId, String mediaId) {

        //远程调用，先判断课程是否存在
        CoursePublish coursepublish = contentServiceClient.getCoursepublish(courseId);
        if (coursepublish == null) {
            return RestResponse.validfail("课程未发布");

        }

        if (StringUtils.isNotEmpty(userId)) {
            XcCourseTablesDto learnStatus = xcChooseCourseService.getLearningStatus(userId, courseId);
            if (learnStatus.equals("702001")) {
                //正常学习
                return mediaServiceClient.getPlayUrlByMediaId(mediaId);
            } else if (learnStatus.equals("702002")) {
                return RestResponse.validfail("无法观看，由于没有选课或选课后没有支付");
            } else if (learnStatus.equals("702003")) {
                return RestResponse.validfail("您的选课已过期需要申请续期或重新支付");
            }
        }
        //未登录
        String charge = coursepublish.getCharge();
        if ("201000".equals(charge)){
            return mediaServiceClient.getPlayUrlByMediaId(mediaId);
        }

        return RestResponse.validfail("请购买课程后继续学习");

    }

}

